import time
import threading

balance = 100
lock = threading.Lock()


def change_it(n):
    global balance
    balance = balance + n
    balance = balance - n


def run_Thread(n):
    for i in range(100000):
        # 先要获取锁:
        lock.acquire()
        try:
            # 放心地改吧:
            change_it(n)
        finally:
            # 改完了一定要释放锁:
            lock.release()


def run_thread2(n):
    for i in range(100000):
        change_it(n)

t1 = threading.Thread(target=run_Thread, args=(5,))
t2 = threading.Thread(target=run_Thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)

t3 = threading.Thread(target=run_thread2, args=(5,))
t4 = threading.Thread(target=run_thread2, args=(8,))
t3.start()
t4.start()
t3.join()
t4.join()
print(balance)
